package DB;

import android.os.Environment;

import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by young on 2016/4/28.
 */
public class FileStoreRegInfo implements Serializable{

    public static File sdCardDir = Environment.getExternalStorageDirectory();
    public static File sdFile = new File(sdCardDir, "registerHistory.out");
    public static List<OrdersBean> orders = new ArrayList<OrdersBean>();

    //新建订单，成功返回true
    public  static  boolean addNewOrdersToFile(OrdersBean orders) {
        boolean flag = false;
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;

        if (orders.getName() == null||orders.getName().equals("")||orders.getId() == null ||orders.getId().equals("")||
                orders.getStarttime() == null ||orders.getStarttime().equals("")||orders.getPrice() == null ||orders.getPrice().equals("")||
                orders.getRoomid() == null ||orders.getRoomid().equals("")||orders.getDays() == null ||orders.getDays().equals("")||
                orders.getPrePay() == null||orders.getPrePay().equals("")) {
            return flag;
        }

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

            try {

                fos = new FileOutputStream(sdFile,true);
                oos = new ObjectOutputStream(fos);
                oos.writeObject(orders);
                flag = true;

            } catch (FileNotFoundException e) {

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (oos != null) {
                    try{
                        oos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return flag;
    }


    //返回当天登记的单条订单信息
    public static OrdersBean querySingleInfo(){
        OrdersBean resultOrdersBean = null;
        FileInputStream fin = null;
        ObjectInputStream oin = null;

        try {
            fin = new FileInputStream(sdFile);
            oin = new ObjectInputStream(fin);
            resultOrdersBean = (OrdersBean)oin.readObject();
            oin.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        finally {
            if (fin != null) {
                try{
                    fin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (oin != null) {
                try {
                    oin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return resultOrdersBean;
    }

    //返回当天登记的所有订单信息
    public static List<OrdersBean> queryAllOrders()throws Exception{
        FileInputStream fin = new FileInputStream(sdFile);
        BufferedInputStream bis = new BufferedInputStream(fin);
        ObjectInputStream oin = null;

        while (true) {
            try {
                oin = new ObjectInputStream(bis);
            } catch (EOFException e) {
                e.printStackTrace();
                System.out.println("已到达文件末尾");
                break;
            }
            Object order = new Object();
            order = oin.readObject();
            if (order instanceof OrdersBean) {
                orders.add((OrdersBean) order);
            }
        }

        if (oin != null) {
            try {
                oin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (bis != null) {
            try{
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fin != null) {
            try{
                fin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return orders;
    }
}
